package com.cuz.hot100;

public class Q63uniquePathsWithObstacles {

    private static int solution1(int[][] arr) {
        if (arr == null || arr.length == 0 || arr[0].length == 0) {
            return 0;
        }
        int rowMax = arr.length;
        int colMax = arr[0].length;
        int[] dp = new int[colMax];
        for (int row = 0; row < rowMax; row++) {
            for (int col = 0; col < colMax; col++) {
                if (row == 0 && col == 0 && arr[row][col] == 0) {
                    dp[col] = 1;
                } else {
                    if (arr[row][col] == 0) {
                        if (col - 1 >= 0 && arr[row][col - 1] == 0) {
                            dp[col] += dp[col - 1];
                        }
                    } else {
                        dp[col]=0;
                    }
                }
            }
        }
        return dp[colMax - 1];
    }

    private static int solution2(int[][] arr) {
        if (arr == null || arr.length == 0 || arr[0].length == 0) {
            return 0;
        }
        int rowMax = arr.length;
        int colMax = arr[0].length;
        int[][] dp = new int[rowMax][colMax];
        for (int row = 0; row < rowMax; row++) {
            for (int col = 0; col < colMax; col++) {
                if (row == 0 && col == 0 && arr[row][col] == 0) {
                    dp[row][col] = 1;
                } else {
                    if (arr[row][col] == 0) {
                        if (row - 1 >= 0) {
                            dp[row][col] += dp[row - 1][col];
                        }
                        if (col - 1 >= 0) {
                            dp[row][col] += dp[row][col - 1];
                        }
                    }
                }
            }
        }
        return dp[rowMax - 1][colMax - 1];
    }
}
